package edu.gatech.math2605.kzhou33.client;
import org.vaadin.gwtgraphics.client.DrawingArea;
import org.vaadin.gwtgraphics.client.Line;
import org.vaadin.gwtgraphics.client.Shape;
import org.vaadin.gwtgraphics.client.animation.Animate;
import org.vaadin.gwtgraphics.client.shape.Circle;
import org.vaadin.gwtgraphics.client.shape.Rectangle;
import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
public class Pinball implements EntryPoint {
private AbsolutePanel lp = new AbsolutePanel();
private Canvas canvas;
private Context2d ctx; // not used now
private int WIDTH = 500, HEIGHT = 500;
private int radius = 50; // radius < s/2
private int side = 250;
private double shift = (HEIGHT - side) / 2;
private double x = WIDTH / 2, y = HEIGHT / 2, dx = 1, dy = 1;
private int ballRadius;
private Circle c1, c2, c3;
private Ball b;
private DrawingArea canvas2;
public void onModuleLoad() {
canvas2 = new DrawingArea(WIDTH, HEIGHT);
lp.add(canvas2);
setup();
Timer t = new Timer() {
@Override
public void run() {
runShape(b);
}
};
t.scheduleRepeating(20);
RootPanel.get().add(lp);
// RootPanel.get().add(new TestView());
}
private void setup() {
c1 = new Circle((int) shift, (int) shift, radius);
c2 = new Circle((int) shift + side, (int) shift, radius);
c3 = new Circle(WIDTH / 2, (int) shift + (int) (1.732058 * side / 2),
radius);
doAll("limegreen");
ball = new Circle(WIDTH / 2, HEIGHT / 2, ballRadius);
ball.setFillColor("black");
int centerShift = (HEIGHT - c1.getY() - c3.getY() + 0 * shift / 2) / 2;
c1.setY(c1.getY() + centerShift);
c2.setY(c2.getY() + centerShift);
c3.setY(c3.getY() + centerShift);
canvas.add(new Rectangle(0, 0, WIDTH, HEIGHT));
canvas.add(c1);
canvas.add(c2);
canvas.add(c3);
canvas.add(ball);
}
private void runShape(Circle s) {
// move
s.setX(s.getX() + dballx);
s.setY(s.getY() + dbally);
// check collision
checkCircleCollision(s, c3);
checkCircleCollision(s, c2);
checkCircleCollision(s, c1);
// check border
if (s.getX() + s.getRadius() > WIDTH || s.getX() - s.getRadius() < 0) {
dballx *= -1;
}
if (s.getY() + s.getRadius() > HEIGHT || s.getY() - s.getRadius() < 0) {
dbally *= -1;
}
}
private void checkCircleCollision(Circle ball, Circle object) {
if (ball.getX() - ball.getRadius() > object.getX() - object.getRadius()
&& ball.getX() + ball.getRadius() < object.getX()
+ object.getRadius()
&& ball.getY() - ball.getRadius() > object.getY()
- object.getRadius()
&& ball.getY() + ball.getRadius() < object.getY()
+ object.getRadius()) {
// GWT.log(ball.getX() + " " + ball.getY());
dballx *= -1;
dbally *= -1;
}
}
public void doAll(String property) {
c1.setFillColor(property);
c2.setFillColor(property);
c3.setFillColor(property);
}
private int getCenterDistance(Shape a, Shape b, int num) {
int x1 = a.getX(), x2 = b.getX(), y1 = a.getY(), y2 = b.getY();
if (num == 1) {
y2 = y1 = 00;
}
if (num == 2) {
x2 = x1 = 0;
}
return (int) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
}